package com.yyfax.pay.depository.service.inner.impl;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.yyfax.framework.common.Log;
import com.yyfax.pay.common.enums.FuiouBusiTypeEnum;
import com.yyfax.pay.common.enums.FuiouDepositRspCodeEnum;
import com.yyfax.pay.common.exception.PayExceptionCode;
import com.yyfax.pay.common.util.FuiouIdUtils;
import com.yyfax.pay.depository.service.inner.ChangeUserInfoNotifyService;
import com.yyfax.pay.depository.service.inner.FuiouChangeCardService;
import com.yyfax.pay.order.model.vo.PayBusiIdVO;
import com.yyfax.pay.third.facade.fuioudeposit.FuiouDepositService;
import com.yyfax.pay.third.facade.fuioudeposit.FuiouSignService;
import com.yyfax.pay.third.model.to.fuioudeposit.BasePlainXMLRsp;
import com.yyfax.pay.third.model.to.fuioudeposit.ChangeUserInfoNotifyReq;
import com.yyfax.pay.third.model.to.fuioudeposit.ResultXMLRsp;

/**
 * 交易通知实现
 * 
 * @author heizq
 * @date 2017-09-07 10:21:42
 * @since v1.0.0
 */
@Service
public class ChangeUserInfoNotifyServiceImpl implements ChangeUserInfoNotifyService {

	Logger logger = LoggerFactory.getLogger(getClass());

	@Resource
	private FuiouIdUtils fuiouIdUtils;
	@Resource
	private FuiouSignService fuiouSignService;

	@Resource
	private FuiouDepositService fuiouDepositService;
	@Resource
	private FuiouChangeCardService changeCardService;

	@Override
	public String changeUserInfoNotify(ChangeUserInfoNotifyReq req) {
		String op = "ChangeUserInfoNotifyServiceImpl.changeUserInfoNotify";
		logger.info(Log.op(op).kv("param", req.toString()).msg("请求参数！").toString());
		// 1.解析业务订单id
		PayBusiIdVO idVO = fuiouIdUtils.parserBusiId(req.getMchnt_txn_ssn());

		boolean flag = fuiouDepositService.checkSignRspResult(idVO.getMerchantCode(), req, req.getSignature());
		if (!flag) {
			throw PayExceptionCode.ERROR_SIGN_FAIL.exp();
		}

		FuiouBusiTypeEnum busiType = idVO.getBusiType();

		switch (busiType) {
		// 更换银行卡
		case USER_CHANGE_CARD:
			changeCardService.handleChangeCardNotify(req, idVO.getMerchantCode());
			break;
		default:
			logger.info(Log.op(op).msg("【富友存管-信息变更通知】业务类型不符！").kv("busiId", req.getMchnt_txn_ssn()).toString());
			break;
		}

		// 封装异步响应
		BasePlainXMLRsp plain = new BasePlainXMLRsp();
		plain.setRspCode(FuiouDepositRspCodeEnum.SUCCESS.getValue());
		plain.setMerchantCode(req.getMchnt_cd());
		plain.setBusiId(req.getMchnt_txn_ssn());

		ResultXMLRsp rsp = new ResultXMLRsp();
		rsp.setPlain(plain);
		rsp.setSign(fuiouSignService.sign(idVO.getMerchantCode(), plain.toXml()));
		logger.info(Log.op(op).kv("rspParams", rsp.toXml()).msg("请求参数！").toString());
		return rsp.toXml();
	}

}
